package com.gxcc3.ygps.web.controller.iot;

import com.gxcc3.ygps.common.annotation.Log;
import com.gxcc3.ygps.common.core.controller.BaseController;
import com.gxcc3.ygps.common.core.domain.AjaxResult;
import com.gxcc3.ygps.common.core.domain.entity.SysIotCar;
import com.gxcc3.ygps.common.core.domain.entity.SysIotStorage;
import com.gxcc3.ygps.common.core.page.TableDataInfo;
import com.gxcc3.ygps.common.enums.BusinessType;
import com.gxcc3.ygps.common.utils.uuid.IdUtils;
import com.gxcc3.ygps.system.service.ISysIotCarService;
import com.gxcc3.ygps.system.service.ISysIotStorageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物联感知 车辆管理
 */
@RestController
@RequestMapping("/iot/car")
public class SysIotCarController extends BaseController {


    @Resource
    ISysIotCarService carService;



    /**
     * 车辆列表
     */
    @PreAuthorize("@ss.hasPermi('iot:car:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysIotCar car)
    {
        startPage();
        car.setCompanyId(getCompanyId());
        List<SysIotStorage> list = carService.selectCarList(car);
        return getDataTable(list);
    }


    /**
     * 新增车辆
     */
    @Log(title = "车辆监测", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @PreAuthorize("@ss.hasPermi('iot:car:add')")
    public AjaxResult add(@Validated @RequestBody SysIotCar car){
        car.setCompanyId(getCompanyId());
        if (carService.checkCarNumberUnique(car)){
            return error("添加车辆失败：当前车辆'" + car.getCarNumber() + "'已录入");
        }
        car.setCreateBy(getUsername());
        car.setReportStatus("1");
        car.setReportTempDeviceNo(IdUtils.randomSnowId());
        car.setReportGpsDeviceNo(IdUtils.randomSnowId());
        return toAjax(carService.insertCar(car));
    }


    /**
     * 获取车辆信息
     */
    @GetMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('iot:car:query')")
    public AjaxResult getCar(@PathVariable Long id){
        return success(carService.selectCarById(id));
    }


    /**
     * 修改车辆信息
     */
    @PostMapping("/edit")
    @PreAuthorize("@ss.hasPermi('iot:car:edit')")
    public AjaxResult edit(@Validated @RequestBody SysIotCar car){
        car.setCreateBy(getUsername());
        return toAjax(carService.updateCar(car));
    }


    /**
     * 修改车辆信息
     */
    @PostMapping("/remove/{ids}")
    @PreAuthorize("@ss.hasPermi('iot:car:remove')")
    public AjaxResult delCar(@PathVariable Long[] ids){
        return toAjax(carService.delCar(ids));
    }
}
